package com.steamedfish.auctionsecurityserveroauth2.service.impl;

import com.steamedfish.auctionsecurityserveroauth2.entiry.RoleEntiry;
import com.steamedfish.auctionsecurityserveroauth2.entiry.RolePermissionEntiry;
import com.steamedfish.auctionsecurityserveroauth2.entiry.UserEntiry;
import com.steamedfish.auctionsecurityserveroauth2.entiry.UserRoleEntiry;
import com.steamedfish.auctionsecurityserveroauth2.mapper.RolePermissionMapper;
import com.steamedfish.auctionsecurityserveroauth2.mapper.UserMapper;
import com.steamedfish.auctionsecurityserveroauth2.mapper.UserRoleMapper;
import com.steamedfish.auctionsecurityserveroauth2.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 高山福
 * @since 2021-03-22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntiry> implements UserService {


    private String ROLE_ID = "role_ca56de48-3592-47d2-820c-45969e03c7bf";
    private String PERMISSION_ID = "permission_684457d6-f9dd-4ce0-b344-823b35a05ab5";
    private String CREATOR = "Register";

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;


    @Override
    public void register(UserEntiry userEntiry) {
        //生成用户表相关字段，生成id
        String userId = "user_" + UUID.randomUUID();
        userEntiry.setPassword(passwordEncoder.encode(userEntiry.getPassword()));

        userEntiry.setId(userId);
        userMapper.insert(userEntiry);
        //生成角色表，生成user_id,role_id
        RolePermissionEntiry rolePermissionEntiry = new RolePermissionEntiry();
        rolePermissionEntiry.setRoleId(ROLE_ID);
        rolePermissionEntiry.setPermissionId(PERMISSION_ID);
        rolePermissionEntiry.setDeleted("0");
        rolePermissionEntiry.setCreateTime(LocalDateTime.now());
        rolePermissionEntiry.setCreator(CREATOR);
//        rolePermissionMapper.insert(rolePermissionEntiry);
        //插入用户角色表id
        UserRoleEntiry userRoleEntiry = new UserRoleEntiry();
        userRoleEntiry.setRoleId(ROLE_ID);
        userRoleEntiry.setUserId(userId);
        userRoleEntiry.setDeleted("0");
        userRoleMapper.insert(userRoleEntiry);
    }
}
